// pages/get-coupons/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList_: [{ id: 0, jine: 100, condition: '满499可用' }, { id: 1, jine: 99, condition: '满199可用' }, { id: 2, jine: 50, condition: '满99可用' }, { id: 2, jine: 50, condition: '满99可用' }, { id: 2, jine: 50, condition: '满99可用' }, { id: 2, jine: 50, condition: '满99可用' }, { id: 2, jine: 50, condition: '满99可用' }, { id: 2, jine: 50, condition: '满99可用' }],
    couponList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 获取优惠券列表
   */
  getCouponList: function () {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/market/coupon/list'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
      },
      success: function (res) {
        console.log('couponlist')
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].value = res.data.data[i].type == 1 ? res.data.data[i].value * 10 : res.data.data[i].value

          }
          that.setData({
            couponList: res.data.data
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
   * 领取优惠券
   */
  getCoupon: function (e) {
    let token = wx.getStorageSync('token')
    let that = this
    let index = e.currentTarget.dataset.index
    console.log('index: ' + index)
    wx.request({
      url: app.apiUrl('/market/coupon/getcoupon'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        id: e.currentTarget.id
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code >= app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.data ? res.data.data : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
          let couponListTemp = that.data.couponList
          for (var i = 0; i < couponListTemp.length; i++) {
            if (i == index) {
              couponListTemp[i].get_status = 1
            }
          }

          that.setData({
            couponList: couponListTemp
          })

        }
        
        

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  getMyCoupons: function () {
    wx.navigateTo({
      url: '/pages/my-coupon-list/index'
    })
  }
})